#ifndef __SPO2_ELECTROME_H
#define __SPO2_ELECTROME_H

#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <stdbool.h>
#include <math.h>


class  Filter  //125HZ采样率，截至频率为5HZ的低通滤波；
{
private:
	double bx[7];
	double ax[6];
public:
	Filter()
	{
		memset(bx, 0, sizeof(bx));
		memset(ax, 0, sizeof(ax));
	}
	float butter(float xn);
};


//#define SampleRate4SpO2 125
class SpO2Electrome
{
private:
	Filter Filt1, Filt2;
	const uint32_t SampleRate4SpO2;
	//float Sig[SampleRate4SpO2 * 10];
	//float Sig0[SampleRate4SpO2 * 10];
	//float Sig2[SampleRate4SpO2 * 10];
	//float Model[SampleRate4SpO2 * 10];
	//float Model2[SampleRate4SpO2 * 10];
	float *Sig, *Sig0, *Sig2, *Model, *Model2;

	float *array1, *array2, *corr_array;	//For locate

	float *sig, *filtered_input;	//For SigQuality

	float *output, *output2;		//For SigProcess

	float model[4];
	int sTart;
	int Q0;
	float locM[4];
	int count;

	int TmDelay_ptr;
	float TmDelay_buf[10];
	int TmDelay2_ptr;
	float TmDelay2_buf[10];

public:
	enum {
		SpO2ElecForceReplaceOn = 1,
		SpO2ElecForceReplaceOff = 0
	};
	SpO2Electrome(uint32_t Sample = 125);
	~SpO2Electrome();
	
	float TM_Delay(float pres);
	float TM_Delay2(float pres);
	int Find_Max(float input[], int length);
	float sMax(float input[], int length);
	float sMin(float input[], int length);
	float sMaxDiff(float input[], int length);
	float sMinDiff(float input[], int length);
	float sMax2(float input[], int length);
	float sMin2(float input[], int length);
	float sMean(float input[], int length);
	void xcorr(float input1[], int m, float input2[], int n, float output[]);
	float conv(float input1[], int m, float input2[], int n, int k);
	int locate(float input1[], float input2[]);
	int SigQuality(float input[], int length);
	int Judge(float model[], float input[]);
	void GetParas(float Model[], float model[]);
	float EnergyRatio(float input[], float input2[]);

	void compensate(float output[], float output2[]);
	int RatioJ(float input[], float input2[]);
	int SigProcess(float input[], float input2[], int nLightAdjust = SpO2ElecForceReplaceOff);
};

extern uint8_t SPO2_Data_ESUProc(float *ir, float *red);



#endif